{% from 'vote.html' import vote %}

{% extends 'layout.html' %}

{# Set variables for base layour #}
{% set active_page = '' %}
{% set title = post.title %}

{% block content %}

    <div class="post">

        <div class="title">
            {% if post.link and post.link|length > 0 %}
                <a href="{{ post.link }}">
                    {{ post.title }}
                </a>
            {% else %}
                {{ post.title }}
            {% endif %}
        </div>
        
        {% if post.link %}
            <div class="netloc">
                {{ post.link|netloc }}
            </div>
        {% endif %}
        
        <div class="topics">
            {% for topic in topics %}
                <a href="{{ url ('topic', name=topic.name) }}" class="topic">{{ topic.name }}</a>
            {% endfor %}
        </div>
        
        <div class="info">
            {{ vote ('post', post, user) }}
            
            <a href="{{ url ('user_public', username=post.username) }}" class="username">
                {{ post.username }}
            </a>
            <time title="{{ post.created|title }}" datetime="{{ post.created|datetime }}">
                <em>{{ post.created|ago }}</em>
            </time>
            
            —
            
            {{ post.vote }} votes, <a href="#comments">{{ post.commentsCount }} comments</a>
            
            —
            
            {% if not show_source %}
                <a href="{{ url ('post', hash_id=post.hashId) }}?source" title="Show unrendered text">Source</a>
            {% endif%}
            
            {% if user and post.userId == user.id %}
                <a href="{{ url ('edit_post', hash_id=post.hashId) }}" title="Edit post">Edit</a>
            {% endif %}
        </div>
        
        <div class="text">
            {% if show_source %}
                <div class="pre">{{ post.text }}</div>
            {% else %}
                {{ post.text|md2html|safe }}
            {% endif %}
        </div>
        
        {# "shortcut-submit" is a class used exclusively from javascript
            # to submit the form when a key (Ctrl+Enter) is pressed.
            #}
        <form action="" method="post" class="new_comment shortcut-submit">
            <textarea
                name="new_comment"
                required="required"
                class="form-control"
                placeholder="{{ 'Write a comment' if user else 'Login to post a comment' }}"
                {{ 'disabled' if not user }}></textarea>
            <input
                type="submit"
                value="Add comment"
                class="button button_info"
                {{ 'disabled' if not user }}/>
        </form>
        
        {# id="" used as anchor #}
        <div class="comments" id="comments">
            {% for comment in comments %}
                {# The id="" is used as anchor #}
                <div class="comment" style="margin-left:{{ comment.depth * 2 }}em" id="comment-{{ comment.hashId }}">
                    <div class="info">
                        {{ vote ('comment', comment, user) }}
                        
                        {# Username #}
                        <span class="username {{ 'op' if post.userId == comment.userId else '' }}">
                            <a href="{{ url ('user_public', username=comment.username) }}">{{ comment.username }}</a>
                        </span>
                        
                        {# DateTime #}
                        <a href="{{ url ('post', hash_id=post.hashId) ~ '#comment-' ~ comment.hashId }}"><time title="{{ comment.created|title }}" datetime="{{ comment.created|datetime }}"><em> {{ comment.created|ago }} </em></time></a>
                        
                        {% if user %}
                            —
                            
                            {# Reply #}
                            <a href="{{ url ('reply', hash_id=comment.hashId) }}">Reply</a>
                            
                            {# Edit #}
                            {% if comment.userId == user.id %}
                                <a href="{{ url ('edit_comment', hash_id=comment.hashId) }}">Edit</a>
                            {% endif %}
                        {% endif %}
                    </div>
                    
                    <div class="text">
                        {% if show_source %}
                            <div class="pre">{{ comment.text }}</div>
                        {% else %}
                            {{ comment.text|md2html|safe }}
                        {% endif %}
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>

{% endblock %}
